package com.partner.frame.support.util;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;

public class UploadUtil {
   /**
    * 
    * @param tempFilePath 缓存文件路径
    * @param httpRequest
    * @param realPath 存放相对路径
    * @return 文件http资源路径,null错误
    * @throws IOException 
    */
   public static String copyFileAndDeleteTemp(String tempFilePath, String realPath, HttpServletRequest httpRequest){
	   if(tempFilePath==null || "".equals(tempFilePath)) return null;
	   File filePreview = new File(tempFilePath);
	   String strPreviewUrl = null;
       if(filePreview.exists())
       {
          try {
			String strPreviewName = filePreview.getName();
			  
			  //预览图片url
			  String strUrl = httpRequest.getScheme() + "://" + httpRequest.getServerName();
			  if(httpRequest.getServerPort() != 80)
			     strUrl = strUrl + ":" + httpRequest.getServerPort();
			  strPreviewUrl = strUrl + httpRequest.getContextPath() + realPath + "/" + strPreviewName;
			  
			  //文件保存目录、保存文件名
			  //String strPreviewPath = httpRequest.getServletContext().getRealPath(realPath);
			 // String strPreviewFile = strPreviewPath + File.separator + strPreviewName;
			  
			  //System.out.println("strPreviewFile[" + strPreviewFile + "]");
			  
			  //检查目录
			  //File filePreviewPath = new File(strPreviewPath);
			 // if(filePreviewPath.exists() == false)
			   //  filePreviewPath.mkdirs();
			  
			  //copy文件
			 // File fileDstPreview = new File(strPreviewFile);
			 // FileUtils.copyFile(filePreview, fileDstPreview);
			  
			  //删除临时文件
			  FileUtils.deleteQuietly(filePreview);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

       }
	   return strPreviewUrl;
   }
}
